package cn.wolfcode.wolf2w.controller;

import cn.wolfcode.wolf2w.annotation.RequiredLogin;
import cn.wolfcode.wolf2w.annotation.UserInfoParam;
import cn.wolfcode.wolf2w.domain.TravelComment;
import cn.wolfcode.wolf2w.domain.Travel;
import cn.wolfcode.wolf2w.domain.TravelComment;
import cn.wolfcode.wolf2w.domain.UserInfo;
import cn.wolfcode.wolf2w.query.TravelCommentQueryObject;
import cn.wolfcode.wolf2w.query.TravelQueryObject;
import cn.wolfcode.wolf2w.service.*;
import cn.wolfcode.wolf2w.util.JsonResult;
import cn.wolfcode.wolf2w.util.UMEditorUploader;
import cn.wolfcode.wolf2w.util.UploadUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/travels")
public class TravelController {

    @Autowired
    private ITravelService travelService;
    @Autowired
    private IDestinationService destinationService;
    @Autowired
    private ITravelCommentService travelCommentService;

    @GetMapping("/query")
    public Object query(TravelQueryObject qo){

        Page page = travelService.query(qo);


        return JsonResult.success(page);
    }

    //上传图片
    @RequestMapping("/coverImageUpload")
    @ResponseBody
    public Object coverImageUpload(MultipartFile pic) throws Exception {
        String upload = UploadUtil.uploadAli(pic);
        return upload;
    }

    //上传图片
    @RequestMapping("/contentImage")
    @ResponseBody
    public String uploadUEImage(MultipartFile upfile, HttpServletRequest request) throws Exception{
        UMEditorUploader up = new UMEditorUploader(request);
        String[] fileType = {".gif" , ".png" , ".jpg" , ".jpeg" , ".bmp"};
        up.setAllowFiles(fileType);
        up.setMaxSize(10000); //单位KB
        up.upload(upfile);

        String callback = request.getParameter("callback");
        String result = "{\"name\":\""+ up.getFileName() +"\", \"originalName\": \""+ up.getOriginalName() +"\", \"size\": "+ up.getSize()
                +", \"state\": \""+ up.getState() +"\", \"type\": \""+ up.getType() +"\", \"url\": \""+ up.getUrl() +"\"}";
        result = result.replaceAll( "\\\\", "\\\\" );
        if(callback == null ){
            return result ;
        }else{
            return "<script>"+ callback +"(" + result + ")</script>";
        }
    }

    @GetMapping("/detail")
    public Object detail(String id){
        return JsonResult.success(travelService.find(id));
    }

    @GetMapping("/dests")
    public Object dests(){

        return JsonResult.success(destinationService.findAll());
    }

    @GetMapping("/input")
    public Object input(String id){
        return JsonResult.success(travelService.find(id));
    }

    @PostMapping("/saveOrUpdate")
    public Object saveOrUpdate(Travel travel, @UserInfoParam UserInfo userInfo){
        travelService.saveOrUpdate(travel,userInfo);
        return JsonResult.success(travel.getId());
    }

    @RequiredLogin
    @PostMapping("/commentAdd")
    public Object commentAdd(TravelComment travelComment, @UserInfoParam UserInfo userInfo){

        travelCommentService.commentAdd(travelComment,userInfo);
        return JsonResult.success();
    }

    @GetMapping("/comments")
    public Object queryTravelComments(String travelId){
        List<TravelComment> travelComments = travelCommentService.queryCommentsByTravelId(travelId);
        return JsonResult.success(travelComments);
    }
}
